package ldh.im.base.service;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import ldh.common.PageResult;
import ldh.im.base.dao.CmsArticleDao;
import ldh.im.base.pojo.CmsArticle;
import ldh.im.base.pojo.where.CmsArticleWhere;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author: ldh
 * @date: 2021-07-04 17:02:34
*/
@Service("cmsArticleService")
public class CmsArticleService {

	@Resource
	private CmsArticleDao cmsArticleDao;
	
	@Transactional
	public CmsArticle insert(CmsArticle cmsArticle) {
		int n = cmsArticleDao.insert(cmsArticle);
		if (n != 1) {
			throw new RuntimeException("CmsArticle insert error!");
		}
		return cmsArticle;
	}
	
	@Transactional
	public CmsArticle insertSelective(CmsArticle cmsArticle) {
		int n = cmsArticleDao.insert(cmsArticle);
		if (n != 1) {
			throw new RuntimeException("CmsArticle insertSelective error!");
		}
		return cmsArticle;
	}

	@Transactional
	public CmsArticle updateById(CmsArticle cmsArticle) {
		int  n = cmsArticleDao.updateById(cmsArticle);
		if (n != 1) {
			throw new RuntimeException("CmsArticle update error!");
		}
        return cmsArticle;
     }

	@Transactional
	public CmsArticle updateNotNullById(CmsArticle cmsArticle) {
		int  n = cmsArticleDao.updateNotNullById(cmsArticle);
		if (n != 1) {
			throw new RuntimeException("CmsArticle update error!");
		}
        return cmsArticle;
     }


    @Transactional
    public void deleteById(Long id) {
		int n = cmsArticleDao.deleteById(id);
		if (n != 1) {
			throw new RuntimeException("CmsArticle delete error!");
		}
    }

	@Transactional(readOnly = true)
	public CmsArticle getById(Long id) {
		CmsArticle cmsArticle = cmsArticleDao.getById(id);
		return cmsArticle;
    }

	@Transactional(readOnly = true)
	public PageResult<CmsArticle> findByCmsArticleWhere(CmsArticleWhere cmsArticleWhere) {
		long total = cmsArticleDao.findTotalByCmsArticleWhere(cmsArticleWhere);
		List<CmsArticle> imageList = new ArrayList<CmsArticle>();
		if (total > 0) {
			imageList  = cmsArticleDao.findByCmsArticleWhere(cmsArticleWhere);
		}
		
		return new PageResult<CmsArticle>(cmsArticleWhere, total, imageList);
	}
}
